package com.panopset.gui.container;

import static com.panopset.Util.x;

/**
 * Integer field.
 *
 * @author Karl Dinwiddie.
 *
 */
public class PanIntField extends PanNumericOnlyField {

    /**
     * Long field message.
     */
    private static final String LONG_FIELD_MESSAGE = x("Please enter")
            + x("an integer value") + ", "
            + x("no decimal places") + ".";

    /**
     * @return Integer value.  0 if the value can't be parsed (blank or
     * something).
     */
    public final Integer getIntValue() {
        try {
            return Integer.parseInt(getValue());
        } catch (NumberFormatException nfe) {
            return 0;
        }
    }

    /**
     * @param cols
     *            Number of columns.
     * @param borderTitle
     *            Border title.
     * @param toolTipText
     *            Tool tip text.
     * @param defaultValue
     *            Default value.
     */
    public PanIntField(final int cols, final String borderTitle,
            final String toolTipText, final String defaultValue) {
        super(cols, borderTitle, toolTipText, defaultValue);
        addValidator(new Validator() {

            @Override
            public String validate(final String value) {
                if (value == null) {
                    return LONG_FIELD_MESSAGE;
                }
                try {
                    Integer.parseInt(value);
                } catch (NumberFormatException ex) {
                    return LONG_FIELD_MESSAGE;
                }
                return null;
            }
        });
    }
}
